
// <copyright file="BinaryFormatterHelper.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   Class BinaryFormatterHelper.This helper class contains few helper methods related to Binary Serialization
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Runtime.Serialization.Formatters.Binary
{
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;

    /// <summary>
    ///     Class BinaryFormatterHelper.This helper class contains few helper methods related to Binary Serialization
    /// </summary>
    public static class BinaryFormatterHelper
    {
        #region Public Methods and Operators

        /// <summary>
        /// The deep clone.
        /// </summary>
        /// <param name="obj">
        /// The obj.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public static object DeepClone(this object obj)
        {
            byte[] bytes = Serialize(obj);
            return Deserialize(bytes, bytes.LongLength);
        }

        /// <summary>
        /// The deserialize.
        /// </summary>
        /// <param name="bytes">
        /// The bytes.
        /// </param>
        /// <param name="length">
        /// The length.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public static object Deserialize(byte[] bytes, long length)
        {
            using (var memStream = new MemoryStream())
            {
                memStream.Write(bytes, 0, (int)length);
                memStream.Seek(0, SeekOrigin.Begin);
                var formatter = new BinaryFormatter();
                return formatter.Deserialize(memStream);
            }
        }

        /// <summary>
        /// The deserialize.
        /// </summary>
        /// <param name="bytes">
        /// The bytes.
        /// </param>
        /// <returns>
        /// The <see cref="object"/>.
        /// </returns>
        public static object Deserialize(byte[] bytes)
        {
            return Deserialize(bytes, bytes.LongLength);
        }

        /// <summary>
        /// The deserialize.
        /// </summary>
        /// <param name="bytes">
        /// The bytes.
        /// </param>
        /// <param name="length">
        /// The length.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public static T Deserialize<T>(byte[] bytes, long length)
        {
            return (T)Deserialize(bytes, length);
        }

        /// <summary>
        /// The deserialize.
        /// </summary>
        /// <param name="bytes">
        /// The bytes.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// The <see cref="T"/>.
        /// </returns>
        public static T Deserialize<T>(byte[] bytes)
        {
            return Deserialize<T>(bytes, bytes.LongLength);
        }

        /// <summary>
        /// Deserializes an object of type T from the the specified file.
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        /// <param name="fileName">
        /// Name of the file.
        /// </param>
        /// <returns>
        /// ``0.
        /// </returns>
        public static T Deserialize<T>(string fileName)
        {
            return (T)Deserialize(fileName);
        }

        /// <summary>
        /// Deserializes an object  from the the specified file.
        /// </summary>
        /// <param name="fileName">
        /// Name of the file.
        /// </param>
        /// <returns>
        /// System.Object.
        /// </returns>
        public static object Deserialize(string fileName)
        {
            using (FileStream fs = File.OpenRead(fileName))
            {
                var formatter = new BinaryFormatter();
                return formatter.Deserialize(fs);
            }
        }

        /// <summary>
        /// Serializes the specified object to the given file.
        /// </summary>
        /// <param name="obj">
        /// The object.
        /// </param>
        /// <param name="fileName">
        /// Name of the file.
        /// </param>
        public static void Serialize(object obj, string fileName)
        {
            using (FileStream fs = File.Create(fileName))
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(fs, obj);
            }
        }

        /// <summary>
        /// Serializes the specified object and returns the result as a byte array
        /// </summary>
        /// <param name="obj">
        /// The object.
        /// </param>
        /// <returns>
        /// System.Byte[][].
        /// </returns>
        public static byte[] Serialize(object obj)
        {
            using (var memStream = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(memStream, obj);
                return memStream.ToArray();
            }
        }

        #endregion Public Methods and Operators
    }
}
